﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Globalization;

namespace HigLabo.Net.Twitter
{
    /// <summary>
    /// 
    /// </summary>
    public static class Func
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static Dictionary<String, Object> CreateDictionary(String json)
        {
            Dictionary<String, Object> result = null;
            if (json != null)
            {
                result = JsonConvert.DeserializeObject<Dictionary<String, Object>>(json);
            }
            return result ?? new Dictionary<String, Object>();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static DateTimeOffset? CastToDateTimeOffset(this Dictionary<String, Object> element, String key)
        {
            DateTimeOffset x = DateTimeOffset.MinValue;
            if (element.ContainsKey(key) == false || element[key] == null)
            { return null; }
            var o = element[key];
            if (DateTimeOffset.TryParseExact(o.ToString()
                , "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out x) == true)
            {
                return x;
            }
            return null;
        }
    }
}
